<template>
	<div ref="mgrDialog" class="dialog-wrap">
		
		<el-dialog 
			:title="headerTitle" 
			width="70%"
			@close="resetDialog" 
			:visible.sync="DialogVisible"
		>
			
			<el-row :gutter="20">
			
				<el-form 
					v-if = "editForm != null"
					ref="dialogForm"
					:rules="rules"
					size="mini"
					:label-width="formLabelWidth"
					:model.sync="editForm">
					
					<el-col :span="24">
						<el-form-item label="标题" prop="title">
		      				<el-input v-model="editForm.title" autocomplete="off"></el-input>
		    			</el-form-item>
		    		</el-col>

		    		<el-col :span="24">	
		    			<el-form-item label="内容" prop="content">
		      				<el-input v-model="editForm.content" type="textarea" autocomplete="off" rows="5"></el-input>
		    			</el-form-item>
		    		</el-col>

		    		<el-col v-if="type==2" :span="12">	
		    			<el-form-item label="发布者">
		      				<el-input v-model="editForm.createrName" readonly disabled autocomplete="off"></el-input>
		    			</el-form-item>
		    		</el-col>

		    		<el-col v-if="type==2" :span="12">	
		    			<el-form-item label="时间">
		      				<el-input v-model="editForm.createTime" readonly disabled autocomplete="off"></el-input>
		    			</el-form-item>
		    		</el-col>
		    			
				</el-form>
			</el-row>
			
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" type="primary" @click="confirm">确定</el-button>
			    <el-button size="mini" type="danger" @click="cancel">取消</el-button>
			</div>	
		</el-dialog>

	</div>
</template>


<script>
import { getRolesTree,getRoleTreeList } from "@/api/admin/roles"
import { getUserInfo } from "@/api/user"

export default{

	props:{
		//新增还是编辑
	    type:{
	      type:[Number,String],
	      defalut:1
	    },
		visible:{
			type:Boolean,
			default:false
		},
		form:{
			type:Object,
			default:null
		}
	},
	watch:{
		form(newVal,oldVal){
			if(newVal != null){
				this.editForm = newVal;
			}else{
				this.editForm = this.defaultForm;
			}	
		},
		type(newVal,oldVal){
	      	if(newVal == 1){
	        	this.headerTitle = "新增通知"
	      	}else if(newVal == 2){
	        	this.headerTitle = "修改通知"
	      	}
	    },
	    visible(val){
	    	this.DialogVisible = val;
	    }
	},
	data(){
		return{
			headerTitle:"新增通知",
			DialogVisible:false,
			defaultForm:{
				title:'',
				content:'',
				createrName:'',
				createTime:'',
			},
			roleTree:[],
			editForm:null,
			formLabelWidth:'100px',
			//验证规则
		    rules:{
		        title:[{required:true,message:"请输入标题",trigger:"blur"}],
		        content:[{required:true,message:"请输入内容",trigger:"blur"}],
		    },
		}
	},
	mounted(){
		this.getRoleTree();
	},
	methods:{
		//获取树形菜单
		getRoleTree(){

			getRoleTreeList().then((res)=>{
				console.log("-----------获取树形菜单-----------")
				console.log(res);
				this.roleTree = res.data;
			})

		},
		resetDialog(){
			this.DialogVisible = false;
			this.editForm = null;
			this.$emit('cancel')
		},
		//取消按钮
		cancel(){
			this.DialogVisible = false;
			this.editForm = null;
			this.$emit('cancel')
		},
		confirm(){
			this.$refs.dialogForm.validate((valid)=>{
		        if (valid) {
		          this.$emit('confirm',{type:this.type,data:this.editForm});
		          this.DialogVisible = false;
		          this.editForm = null;
		        } else {
		            return false;
		        }

      		})
		}

	}
}


</script>

<style scoped>
	
</style>